package com.ruoyi.supermarket.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.supermarket.bean.SelectedCategoryRes;
import com.ruoyi.supermarket.domain.Category;
import com.ruoyi.supermarket.service.CategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.SelectedCategory;
import com.ruoyi.supermarket.service.SelectedCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 精选分类Controller
 * 
 * @author 李其伟
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/supermarket/selectedCategory")
public class SelectedCategoryController extends BaseController
{
    @Autowired
    private SelectedCategoryService selectedCategoryService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询精选分类列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:selectedCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SelectedCategory selectedCategory)
    {
        startPage();
        List<SelectedCategory> list = selectedCategoryService.selectSelectedCategoryList(selectedCategory);
        TableDataInfo tableDataInfo =  getDataTable(list);
        List<SelectedCategory> selectedCategoryList = (List<SelectedCategory>) tableDataInfo.getRows();

        if(selectedCategoryList.size()==0){
            return tableDataInfo;
        }

        List<Category> categoryList = categoryService.selectCategoryList(new Category());
        Map<Long,Category> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId, category->category));

        List<SelectedCategoryRes> selectedCategoryResList = new ArrayList<>();
        for(SelectedCategory sc : selectedCategoryList){
            SelectedCategoryRes res = new SelectedCategoryRes();
            res.setCategory(categoryMap.get(sc.getCategoryId()));
            res.setSelectedCategory(sc);
            selectedCategoryResList.add(res);
        }
        tableDataInfo.setRows(selectedCategoryResList);
        return tableDataInfo;
    }

    /**
     * 导出精选分类列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:selectedCategory:export')")
    @Log(title = "精选分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SelectedCategory selectedCategory)
    {
        List<SelectedCategory> list = selectedCategoryService.selectSelectedCategoryList(selectedCategory);
        ExcelUtil<SelectedCategory> util = new ExcelUtil<SelectedCategory>(SelectedCategory.class);
        util.exportExcel(response, list, "精选分类数据");
    }

    /**
     * 获取精选分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:selectedCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(selectedCategoryService.selectSelectedCategoryById(id));
    }

    /**
     * 新增精选分类
     */
    @PreAuthorize("@ss.hasPermi('supermarket:selectedCategory:add')")
    @Log(title = "精选分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SelectedCategory selectedCategory)
    {
        return toAjax(selectedCategoryService.insertSelectedCategory(selectedCategory));
    }

    /**
     * 修改精选分类
     */
    @PreAuthorize("@ss.hasPermi('supermarket:selectedCategory:edit')")
    @Log(title = "精选分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SelectedCategory selectedCategory)
    {
        return toAjax(selectedCategoryService.updateSelectedCategory(selectedCategory));
    }

    /**
     * 上下架精选分类
     */
    @PreAuthorize("@ss.hasPermi('supermarket:selectedCategory:publish')")
    @Log(title = "精选分类", businessType = BusinessType.GRANT)
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SelectedCategory selectedCategory)
    {
        return toAjax(selectedCategoryService.publishSelectedCategory(selectedCategory));
    }

    /**
     * 删除精选分类
     */
    @PreAuthorize("@ss.hasPermi('supermarket:selectedCategory:remove')")
    @Log(title = "精选分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(selectedCategoryService.deleteSelectedCategoryByIds(ids));
    }
}
